package com.sy.simpleegg.platform

import android.content.Context
import com.sy.simpleegg.bean.CashConfigBean
import com.sy.simpleegg.bean.MotorConfigBean
import com.sy.simpleegg.bean.RegDataBean
import com.sy.simpleegg.platform.defmqtt.bean.DefMqttParam
import com.sy.simpleegg.platform.kkb.bean.KkbSdkParam
import com.sy.simpleegg.platform.kkb.bean.RecieveOrderKkbBean
import com.sy.simpleegg.platform.kkb.bean.member.KkbQrCodeMemberInfoResponse
import com.sy.simpleegg.platform.lyybox.bean.LyyBoxParam
import com.sy.simpleegg.platform.lyysdk.bean.LyySdkParam
import com.sy.simpleegg.platform.whm.bean.WhmMqttParam
import com.sy.simpleegg.platform.whmhex.bean.WhmMqttHexParam
import com.sy.simpleegg.platform.ychsdk.bean.RecieveOrderYchBean
import com.sy.simpleegg.platform.ychsdk.bean.YchSdkParam
import com.sy.simpleegg.platform.ychsdk.bean.member.QrCodeMemberInfoResponse
import com.sy.mqhexsdk.bean.rec.RecUserInfoNew


class MainManager(
    private val context: Context,
    private val defMqParam: DefMqttParam?,
    private val lyySdkParam: LyySdkParam?,
    private val lyyBoxParam: LyyBoxParam?,
    private val whmMqttParam: WhmMqttParam?,
    private val whmMqttHexParam: WhmMqttHexParam?,
    private val ychSdkParam: YchSdkParam?,
    private val kkbSdkParam: KkbSdkParam?,
    private var backstageCallback: OnBackstageCallback?,
) : BaseManager(context, defMqParam, lyySdkParam, lyyBoxParam, whmMqttParam, whmMqttHexParam, ychSdkParam, kkbSdkParam) {

    private var isUpload = false

    val isDefMqttRunning: Boolean
        get() = defMqParam != null

    val isLyySdkRunning: Boolean
        get() = lyySdkParam != null

    val isLyyBoxRunning: Boolean
        get() = lyyBoxParam != null

    val isWhmMqttRunning: Boolean
        get() = whmMqttParam != null

    val isWhmMqttHexRunning: Boolean
        get() = whmMqttHexParam != null

    override fun toLogin() {
        backstageCallback?.toLogin()
    }

    override fun onLoginSuccess(platform: BackstagePlatform, devNumber: String?) {
        backstageCallback?.onLoginSuccess(platform, devNumber)
        when (platform) {
            BackstagePlatform.DEF_MQTT -> {

            }

            BackstagePlatform.LYY_SDK -> {
                if (isUpload) {
                    return
                }
                isUpload = true
            }

            BackstagePlatform.LYY_BOX -> {}
            BackstagePlatform.WHM_MQTT -> {}
            BackstagePlatform.WHM_MQTT_HEX -> {

            }

            BackstagePlatform.YCH_SDK -> {

            }
            BackstagePlatform.KKB -> {

            }
        }
    }

    override fun onConnectionChange(platform: BackstagePlatform, connectState: Boolean) {
        backstageCallback?.onConnectionChange(platform, connectState)
    }

    override fun onGetPackage(platform: BackstagePlatform, pkgCount: Int, pkgType: Int) {
        backstageCallback?.onGetPackage(platform, pkgCount, pkgType)
    }

    override fun onRefreshPackageData(platform: BackstagePlatform, dataList: Any) {
        backstageCallback?.onRefreshPackageData(platform, dataList)
    }

    override fun onBillResp(platform: BackstagePlatform, sucess: Boolean, billMsgNumber: String) {
        backstageCallback?.onBillResp(platform, sucess, billMsgNumber)
    }

    override fun onShowPayCode(platform: BackstagePlatform, codeData: String?, showType: Int) {
        backstageCallback?.onShowPayCode(platform, codeData, showType)
    }

    override fun onSaveCoinResult(platform: BackstagePlatform, result: Boolean) {
        backstageCallback?.onSaveCoinResult(platform, result)
    }

    override fun onConsumeCoinResult(platform: BackstagePlatform, success: Boolean, errMsg: String?, memberId: String?, balance: Long?) {
        backstageCallback?.onConsumeCoinResult(platform, success, errMsg, memberId, balance)
    }

    override fun onReceiveOrder(platform: BackstagePlatform, orderData: Any) {
        backstageCallback?.onReceiveOrder(platform, orderData)
    }

    override fun onReceiveWithdrawalCoinOrder(platform: BackstagePlatform, orderData: Any) {
        backstageCallback?.onReceiveWithdrawalCoinOrder(platform, orderData)
    }

    override fun onReceiveWriteOffOrder(platform: BackstagePlatform, orderData: Any) {
        backstageCallback?.onReceiveWriteOffOrder(platform, orderData)
    }

    override fun onReceiveOrderYchHttp(
        platform: BackstagePlatform,
        orderDataYch: RecieveOrderYchBean,
        paySuccessed: Boolean
    ) {
        backstageCallback?.onReceiveOrderYchHttp(platform, orderDataYch, paySuccessed)
    }

    override fun onReceiveOrderKkbHttp(
        platform: BackstagePlatform,
        orderDataKkb: RecieveOrderKkbBean,
        paySuccessed: Boolean
    ) {
        backstageCallback?.onReceiveOrderKkbHttp(platform, orderDataKkb, paySuccessed)
    }

    override fun onShowTips(platform: BackstagePlatform, colorType: Int, tips: String) {
        backstageCallback?.onShowTips(platform, colorType, tips)
    }

    override fun onShowRegist(platform: BackstagePlatform, isShow: Boolean, registCode: String?) {
        backstageCallback?.onShowRegist(platform, isShow, registCode)
    }

    override fun lyyRespLyyBoardOutResult(platform: BackstagePlatform, dataHex: String) {
        backstageCallback?.lyyRespLyyBoardOutResult(dataHex)
    }

    override fun lyyRespListAdd(platform: BackstagePlatform, dataHex: String) {
        backstageCallback?.lyyRespListAdd(dataHex)
    }

    override fun lyyRespBoxNetwork(platform: BackstagePlatform, dataHex: String) {
        backstageCallback?.lyyRespBoxNetwork(dataHex)
    }

    override fun lyyCheckBoardConnecton(platform: BackstagePlatform, dataHex: String, uniqueCodeHex: String) {
        backstageCallback?.lyyCheckBoardConnecton(dataHex, uniqueCodeHex)
    }

    override fun lyyGetBoardState(platform: BackstagePlatform, dataHex: String, uniqueCodeHex: String) {
        backstageCallback?.lyyGetBoardState(dataHex, uniqueCodeHex)
    }

    override fun lyyGetBoardConfig(platform: BackstagePlatform, dataHex: String) {
        backstageCallback?.lyyGetBoardConfig(dataHex)
    }

    override fun lyySetBoardConfig(platform: BackstagePlatform, dataHex: String) {
        backstageCallback?.lyySetBoardConfig(dataHex)
    }

    override fun lyyRespBoxHeartBeat(platform: BackstagePlatform, heartBeat: Boolean) {
        backstageCallback?.lyyBoxHeartBeat(heartBeat)
    }

    override fun onRestartApp() {
        backstageCallback?.onRestartApp()
    }

    override fun onRecCashConfig(cashConfigBean: CashConfigBean) {
        backstageCallback?.onRecCashConfig(cashConfigBean)
    }

    override fun onRecMotorConfig(motorConfigBean: MotorConfigBean) {
        backstageCallback?.onRecMotorConfig(motorConfigBean)
    }

    override fun getBasicConfigToResp(snHex: String) {
        backstageCallback?.getBasicConfigToResp(snHex)
    }

    override fun getCashConfigToResp(snHex: String) {
        backstageCallback?.getCashConfigToResp(snHex)
    }

    override fun getMotorConfigToResp(snHex: String) {
        backstageCallback?.getMotorConfigToResp(snHex)
    }

    override fun onRecBasicConfig(basicConfigHex: String) {
        backstageCallback?.onRecBasicConfig(basicConfigHex)
    }

    override fun onUserLoginDev(recUserInfoNew: RecUserInfoNew) {
        backstageCallback?.onUserLoginDev(recUserInfoNew)
    }

    override fun onUserLoginDevYch(loginMemberInfo: QrCodeMemberInfoResponse) {
        backstageCallback?.onUserLoginDevYch(loginMemberInfo)
    }

    override fun onUserLoginDevKkb(loginMemberInfo: KkbQrCodeMemberInfoResponse) {
        backstageCallback?.onUserLoginDevKkb(loginMemberInfo)
    }

    override fun onShowLoginCode(code: String) {
        backstageCallback?.onShowLoginCode(code)
    }

    override fun onShowWithdrawalCode(code: String) {
        backstageCallback?.onShowWithdrawalCode(code)
    }

    override fun onRecDevConsumeCode(code: String) {
        backstageCallback?.onRecDevConsumeCode(code)
    }

    override fun onDevLogoutResp() {
        backstageCallback?.onDevLogoutResp()
    }

    override fun placeOrder(
        platform: BackstagePlatform,
        transId: String,
        orderId: String,
        orderNumber: String,
        goodId: String,
        coins: String,
        money: String
    ) {
        backstageCallback?.onPlaceOrder(platform, transId, orderId, orderNumber, goodId, coins, money)
    }

    override fun placeCashOrderResp(
        platform: BackstagePlatform,
        transId: String,
        orderId: String,
        orderNumber: String,
        goodId: String,
        coins: String,
        money: String
    ) {
        backstageCallback?.onPlaceCashOrder(platform, transId, orderId, orderNumber, goodId, coins, money)
    }

    override fun onPostRegData(regDataList: MutableList<RegDataBean>) {
        backstageCallback?.onPostRegData(regDataList)
    }

    override fun onWriteOffResult(platform: BackstagePlatform, writeOffResp: Any) {
        backstageCallback?.onWriteOffResult(platform, writeOffResp)
    }

    override fun onUpgrade(fileId: String, name: String, url: String) {
        backstageCallback?.onUpgrade(fileId, name, url)
    }

    override fun onUploadLog(logType: Int, logNumber: String) {
        backstageCallback?.onUploadLog(logType, logNumber)
    }

    interface OnBackstageCallback {
        /**
         * mqtt执行登录指令
         */
        fun toLogin()

        /**
         * 登录成功
         * @param platform 登录平台
         * @param devNumber 设备号
         */
        fun onLoginSuccess(platform: BackstagePlatform, devNumber: String?)

        /**
         * 链接状态改变
         * @param platform 平台
         * @param connectState 链接专题
         */
        fun onConnectionChange(platform: BackstagePlatform, connectState: Boolean)

        /**
         * 套餐数据
         * @param platform 平台
         * @param pkgCount 套餐数量
         * @param pkgType 套餐类型
         */
        fun onGetPackage(platform: BackstagePlatform, pkgCount: Int, pkgType: Int)

        /**
         * 刷新套餐数据
         * @param platform 平台
         * @param dataList 数据列表
         */
        fun onRefreshPackageData(platform: BackstagePlatform, dataList: Any)

        /**
         * 投钞数据响应
         * @param platform 平台
         * @param sucess 平台
         * @param billMsgNumber 消息编号
         */
        fun onBillResp(platform: BackstagePlatform, sucess: Boolean, billMsgNumber: String)

        /**
         * 收到订单
         * @param platform 平台
         * @param orderEntity 订单数据
         * @param msg
         */
//        fun onReceiveOrder(platform: BackstagePlatform, orderEntity: OrderEntity, msg: String?)

        /**
         * 显示二维码或者图片
         * @param platform 平台
         * @param codeData 显示数据
         * @param showType 显示类型 0-二维码 1-图片
         */
        fun onShowPayCode(platform: BackstagePlatform, codeData: String?, showType: Int)

        /**
         * 上报存币数据响应
         * @param platform 平台
         * @param result 后台响应上报结果
         */
        fun onSaveCoinResult(platform: BackstagePlatform, result: Boolean)

        /**
         * 上报取币数据响应
         * @param platform 平台
         * @param result 后台响应上报结果
         */
        fun onConsumeCoinResult(platform: BackstagePlatform, success: Boolean, errMsg: String?, memberId: String?, balance: Long?)

        /**
         * 支付成功订单
         * @param platform 平台
         * @param k 订单号
         * @param coins 出币数
         */
        fun onReceiveOrder(platform: BackstagePlatform, orderData: Any)

        /**
         * 提币订单
         * @param platform 平台
         * @param k 订单号
         */
        fun onReceiveWithdrawalCoinOrder(platform: BackstagePlatform, orderData: Any)

        /**
         * 核销订单
         * @param platform 平台
         * @param k 订单号
         */
        fun onReceiveWriteOffOrder(platform: BackstagePlatform, orderData: Any)

        /**
         * 油菜花查询订单支付成功
         * @param platform 平台
         * @param orderDataYch 订单数据
         */
        fun onReceiveOrderYchHttp(platform: BackstagePlatform, orderDataYch: RecieveOrderYchBean, paySuccessed: Boolean)

        /**
         * 酷酷宝查询订单支付成功
         * @param platform 平台
         * @param orderDataYch 订单数据
         */
        fun onReceiveOrderKkbHttp(platform: BackstagePlatform, orderDataKkb: RecieveOrderKkbBean, paySuccessed: Boolean)

        /**
         * 显示提示信息
         * @param platform 平台
         * @param colorType 文本颜色类型 1-黑色 其他-红色
         * @param tips 显示内容
         */
        fun onShowTips(platform: BackstagePlatform, colorType: Int, tips: String)

        /**
         * 显示注册码
         * @param platform 平台
         * @param isShow 0-显示 1-隐藏
         * @param registCode 注册二维码
         */
        fun onShowRegist(platform: BackstagePlatform, isShow: Boolean, registCode: String?)

        /**
         * lyy盒子出币结果控制板
         */
        fun lyyRespLyyBoardOutResult(dataHex: String)
        /**
         * lyy盒子下发支付二维码连接
         * @param codeDataHex 二维码内容
         */
//        fun lyyToShowPayQrCode(codeDataHex: String)
        /**
         * lyy盒子回复lyy控制板账目增量
         */
        fun lyyRespListAdd(dataHex: String)

        /**
         * lyy盒子回复lyy控制板网络状态
         */
        fun lyyRespBoxNetwork(dataHex: String)

        /**
         * lyy盒子控制板链接状态
         * @param dataHex 盒子下发的完整16进制数据，如果控制板是lyy的，可以直接转发
         * @param uniqueCodeHex 唯一码 非lyy控制板使用
         */
        fun lyyCheckBoardConnecton(dataHex: String, uniqueCodeHex: String)

        /**
         * lyy盒子查询控制板运行状态
         * @param dataHex 盒子下发的完整16进制数据，如果控制板是lyy的，可以直接转发
         * @param uniqueCodeHex 唯一码 非lyy控制板使用
         */
        fun lyyGetBoardState(dataHex: String, uniqueCodeHex: String)

        /**
         * lyy盒子获取控制板参数
         */
        fun lyyGetBoardConfig(dataHex: String)

        /**
         * lyy盒子设置控制板参数
         */
        fun lyySetBoardConfig(dataHex: String)

        /**
         * lyy盒子心跳
         */
        fun lyyBoxHeartBeat(heartBeat: Boolean)

        /**
         * 重启APP
         */
        fun onRestartApp()

        /**
         * 收到纸钞配置
         * @param cashConfigBean
         */
        fun onRecCashConfig(cashConfigBean: CashConfigBean)

        fun onRecMotorConfig(motorConfigBean: MotorConfigBean)

        fun getBasicConfigToResp(snHex: String)

        fun getCashConfigToResp(snHex: String)

        fun getMotorConfigToResp(snHex: String)

        fun onRecBasicConfig(basicConfigHex: String)

        fun onUserLoginDev(recUserInfo: RecUserInfoNew)

        fun onUserLoginDevYch(loginMemberInfo: QrCodeMemberInfoResponse)

        fun onUserLoginDevKkb(loginMemberInfo: KkbQrCodeMemberInfoResponse)

        fun onShowLoginCode(code: String)

        fun onShowWithdrawalCode(code: String)

        fun onRecDevConsumeCode(code: String)

        fun onDevLogoutResp()

        fun onPlaceOrder(platform: BackstagePlatform, transId: String, orderId: String, orderNumber: String, goodId: String, coins: String, money: String)

        fun onPlaceCashOrder(platform: BackstagePlatform, transId: String, orderId: String, orderNumber: String, goodId: String, coins: String, money: String)

        fun onPostRegData(regDataList: MutableList<RegDataBean>)

        fun onWriteOffResult(platform: BackstagePlatform, writeOffResp: Any)

        fun onUpgrade(fileId: String, name: String, url: String)

        fun onUploadLog(logType: Int, logNumber: String)
    }
}